from django.db import models
from main.models import Book
from user.models import CustomUser


# Create your models here.
class Collect(models.Model):
    user = models.ForeignKey(verbose_name="用户", to=CustomUser, on_delete=models.CASCADE)
    book = models.ForeignKey(verbose_name="书", to=Book, on_delete=models.CASCADE)
    time = models.DateTimeField(verbose_name="收藏事件", auto_now_add=True)

    def __str__(self):
        return f"{self.user.username}收藏了{self.book.title}"

    class Meta:
        verbose_name = "收藏"
        verbose_name_plural = "收藏"
        # 组合唯一，('user', 'book') 字段的组合必须是唯一的。
        unique_together = [('user', 'book')]
